<?php

namespace controller;

/**
 * klasa do zarządzania kategoriami w panelu admina sklepu
 *
 * @author Damian
 */
class ShopAdmin_Category extends Seller {

    private $categoryID;
    private $category;

    public function __construct($options = array()) {
        parent::__construct($options);
        $view = $this->getActionView();        
        $session = \library\Registry::get("session");
        if (\library\RequestMethods::get("categoryID", null) == null || \library\RequestMethods::get("categoryID") == '') {
            $this->category = \model\TblCategory::getModel()->getByURL("kategoria_glowna", $this->shop->ShopID);
            $this->categoryID = $this->category->CategoryID;
            $view->set("active_category", $this->category->Name);
            $regRef = $session->get("regRef");
            $cat = $session->get("categoryID");
            if ($cat != null) {
                $this->categoryID = $cat;
                if ($this->category->ParentCategoryID == null && $regRef == null) {
                    $this->categoryID = $this->category->CategoryID;
                    $session->erase("regRef");
                }
            }
            $session->set("categoryID", $this->categoryID);
        } else {
            $categoryID = (int) \library\RequestMethods::get("categoryID");
            $category = \model\TblCategory::getModel()->getByPrimaryKey($categoryID);
            if ($category == null) {
                throw new exceptions\CategoryNotFoundException("wybrana kategoria nie istnieje w danym sklepie");
            }
            $this->categoryID = $category->CategoryID;
            $view->set("categoryId", $this->categoryID);
            $this->category = \model\TblCategory::getModel()->getByPrimaryKey($this->categoryID);
            $view->set("active_category", $this->category->Name);
            $session->set("categoryID", $this->categoryID);
        }
    }

    public function index() {
        $categories = $this->category->getChildren();
        $layoutView = $this->getLayoutView();
         $layoutView->set("paneltitle","wybrana kategoria ".$this->category->Name);
        $categoriesDataTable = new \model\DataTable($categories);
        $categoriesModelData = $categoriesDataTable->getModelData();
        $view = $this->getActionView();
        $list = $this->category->createBreadCramps();
        $IDs = $this->category->getAllIds();
        $NumberProducts = \model\TblProduct::getModel()->createCriteria($this->shop->ShopID)->in(\model\TblProduct::CATEGORY_ID, $IDs,  \model\TblCategory::getModel()->getTable(), " and ")->count();
        $NumberVisableProducts =  \model\TblProduct::getModel()->createCriteria($this->shop->ShopID)
                ->where(\model\TblProduct::ACTIVE, "=", 1,false," and ")->in(\model\TblProduct::CATEGORY_ID, $IDs,  \model\TblCategory::getModel()->getTable(), " and ")->count();
        $view->set("NumberProducts", $NumberProducts);
        $view->set("NumberVisableProducts", $NumberVisableProducts);
        $view->set("activeCategory",  $this->category->CategoryID);
        $view->set("CategoryList", $list);
        if ($categories) {
            $view->set("categories", $categoriesModelData);
        }
        $this->render();
    }

    public function add() {
        $layoutView = $this->getLayoutView();
        $layoutView->set("paneltitle","dodajesz kategorię do podkategori ".$this->category->Name);
        $name = \library\RequestMethods::post("inputShopCategoryName", null);
        $description = \library\RequestMethods::post("opisPelny", null);
        $session = \library\Registry::get("session");
        $parentCategory = \model\TblCategory::getModel()->getByPrimaryKey($session->get("categoryID"));
        $depth = ($parentCategory != null) ? $parentCategory->Depth + 1 : 1;
        if ($name != null && $description != null) {
            $category = new \model\TblCategory([
                \model\TblCategory::PARENT_CATEGORY_ID => $session->get("categoryID"),
                \model\TblCategory::NAME => $name,
                \model\TblCategory::DESCRIPTION => $description,
                \model\TblCategory::SHOP_ID => $this->shop->ShopID,
                \model\TblCategory::DEPTH => $depth,
                \model\TblCategory::HAS_CHILDREN => 0
            ]);
            $category->generateURL();
            if ($category->validate()) {
                try {
                    $category->save();
                    $cat = $session->get("categoryID");
                    $parentCategory->HasChildren = 1;
                    $parentCategory->save();
                    $this->redirect($this->shop->URL . "/ShopAdmin_Category/{$cat}");
                } catch (\model\exceptions\ExceptionSQLQUery $ex) {
                    $session->erase("categoryID");
                    if ($ex->getCode() == 1062) {
                        $errors = array();
                        $errors["Name"][] = "Kategoria o takiej nazwie już istnieje";
                        $view = $this->getActionView();
                        $view->set("errors", $errors);
                        $this->render();
                    } else {
                        throw $ex;
                    }
                }
            } else {

                $view = $this->getActionView();
                $view->set("errors", $category->getErrors());
                var_dump($category->getErrors());
                die();
                $session->set("regRef", "1");
                $session->set("categoryID", $this->categoryID);
                $this->render();
            }
        } else {
            $this->render();
        }
    }

    public function edit() {
        $view = $this->getActionView();
        $view->set("category", $this->category);
         $layoutView = $this->getLayoutView();
         $layoutView->set("paneltitle","Edycja kategorii ".$this->category->Name);
        $name = \library\RequestMethods::post("inputShopCategoryName", null);
        $description = \library\RequestMethods::post("opisPelny", null);
        $session = \library\Registry::get("session");
        if ($name != null && $description != null) {
            $this->category->Name = $name;
            $this->category->Description = $description;
            $this->category->generateURL();
            if ($this->category->validate()) {
                $this->category->save();
                $cat = $session->get("categoryID");
                $this->redirect($this->shop->URL . "/ShopAdmin_Category/{$cat}");
            } else {
                $view->set("errors", $this->getErrors());
                $session->set("regRef", "1");
                $session->set("categoryID", $this->categoryID);
                $this->render();
            }
        } else {
            $this->render();
        }
        $this->render();
    }
    public function features(){
         $session = \library\Registry::get("session");
         $list = $this->category->createBreadCramps();
         $view = $this->getActionView();
         $view->set("CategoryList", $list);
         $layoutView = $this->getLayoutView();
         $layoutView->set("paneltitle","Cechy kategorii ".$this->category->Name);
         $features = \model\TblCategoryFeature::getModel()->getAllByCategory($this->category);
         if ($features){
             $view->set("features",$features);
         }
         $name = \library\RequestMethods::post("inputFeatureName", null);
         if ($name){
             $Feature = new \model\TblCategoryFeature([
             \model\TblCategoryFeature::NAME=>$name,
             \model\TblCategoryFeature::CATEGORY_ID=>  $this->category->CategoryID,
             \model\TblCategoryFeature::SHOP_ID=>  $this->shop->ShopID
             ]);
             if ($Feature->validate()){
                 $Feature->save();
             }
             else {
                $view = $this->getActionView();
                $view->set("errors", $this->getErrors());
                $session->set("categoryID", $this->categoryID);
                $this->render();
            }
         }
         $this->render();
    }

}
